package cc.minos.components.container
{
	import cc.minos.components.common.Component;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class Stack extends Component
	{
		protected var _selectedChild:DisplayObject;
		protected var _selectedIndex:Number;
		
		protected var views:Vector.<DisplayObject>;
		
		public function Stack()
		{
			super();
			this._type = 'stack';
			views = new Vector.<DisplayObject>();
			addEventListener( Event.REMOVED_FROM_STAGE , onRemove );
		}
		
		public function removeAllChildren():void
		{
			views.length = 0;
			while ( numChildren > 0 )
				removeChildAt( 0 );
		}
		
		override public function addChild( child:DisplayObject ):flash.display.DisplayObject
		{
			var tempChild:DisplayObject = super.addChild( child );
			views.push( child );
			if ( views.length > 1 )
			{
				child.visible = false;
			}
			else
			{
				child.visible = true;
				selectedChild = child;
				selectedIndex = 0;
			}
			return tempChild;
		}
		
		public function set selectedChild( child:DisplayObject ):void
		{
			for ( var a:Number = 0 ; a < numChildren ; a++ )
			{
				getChildAt( a ).visible = false;
			}
			_selectedChild = child;
			_selectedChild.visible = true;
			_selectedIndex = getChildIndex( child );
		}
		
		public function get selectedChild():DisplayObject
		{
			return _selectedChild;
		}
		
		public function set selectedIndex( value:Number ):void
		{
			selectedChild = getChildAt( value );
			_selectedIndex = value;
		}
		
		public function get selectedIndex():Number
		{
			return _selectedIndex;
		}
		
		protected function onRemove( e:Event ):void
		{
			removeEventListener( Event.REMOVED_FROM_STAGE , onRemove );
			removeAllChildren();
		}
	
	}

}